package com.szsh.aiot.framework.spring.interceptor;

import com.szsh.aiot.common.utils.StringUtils;
import com.szsh.aiot.framework.constant.Constants;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

public class TraceIdInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 将流水号设置为流程ID中
        MDC.put(Constants.TRACE_ID, "[" + UUID.randomUUID() + "-" + StringUtils.nvl(request.getParameter(Constants.SEQUENCE_NO)) + "]");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
